import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.border.TitledBorder;
import javax.swing.event.*;
import javax.swing.table.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;

public class CompareData extends JDialog 
{
	public CompareData(JFrame frame,Patient patient_)
	{
		super(frame,patient_.getLastName() + " " + patient_.getName(),true);
		patient = patient_;
		JPanel mainPanel = new JPanel(new BorderLayout());
		Border etched = BorderFactory.createEtchedBorder();
        Border titled = BorderFactory.createTitledBorder(
            etched, 
            Resources.getString("dataCompare"),
            TitledBorder.LEFT, 
            TitledBorder.TOP, 
            Resources.getFont(Resources.FONT_TYPE.BORDER)
        );
        mainPanel.setBorder(titled);
		JSplitPane mainSplitPane;
		JSplitPane secondSplitPane;
		secondSplitPane = new JSplitPane( 
                JSplitPane.HORIZONTAL_SPLIT, 
                false, 
                getSelectionPanel(), 
                getGraphicPanel());
		secondSplitPane.setOneTouchExpandable( true);
		secondSplitPane.setDividerSize(10);
		secondSplitPane.setDividerLocation(300);
		mainSplitPane = new JSplitPane( 
                JSplitPane.VERTICAL_SPLIT, 
                false, 
                secondSplitPane, 
                getTablePanel());
		mainSplitPane.setOneTouchExpandable( true);
		mainSplitPane.setDividerSize(10);
		mainSplitPane.setDividerLocation(350);
		mainPanel.add(mainSplitPane,BorderLayout.CENTER);
		this.add(mainPanel);
		this.setSize(750, 600); 
	}
	private JPanel getSelectionPanel()
	{
		JPanel selectionPanel = new JPanel(new BorderLayout());
		JPanel panel_aux = new JPanel(new BorderLayout());
		final CalendarGraphic year = new CalendarGraphic(patient.getDate());
		year.setDaysMarked(patient.getDateList());
		year.setSingleSelection(false);
		year.setNormalSelectionMode(false);
		JButton addButton = new JButton(Resources.getString("add"));
		addButton.addActionListener(new 
			ActionListener()
			{
				public void actionPerformed(ActionEvent e) 
				{						
					Date[] days = year.getSelectedDays();
					somaGraph.clear();
					if(days != null)
					{
						addPointsToGraph(days);
						
					}
					addRowsToTable(days);
					somaGraph.update();
				}
			});
		JButton selectAllButton = new JButton(Resources.getString("selectAll"));
		selectAllButton.addActionListener(new 
				ActionListener()
    			{
					public void actionPerformed(ActionEvent e) 
					{
						year.setSelectedDays(patient.getDateList());
					}
    			});
		JPanel panelSouth = new JPanel();
		panelSouth.add(addButton);
		panelSouth.add(selectAllButton);
		
		panel_aux.add(year.getPanel(),BorderLayout.CENTER);
		panel_aux.add(panelSouth,BorderLayout.SOUTH);
		selectionPanel.add(panel_aux,BorderLayout.NORTH);
		return selectionPanel;
	}
	private JPanel getGraphicPanel()
	{
		JPanel graphicPanel = new JPanel(new BorderLayout(15,15));
		somaGraph = new Somatochart();
		somaGraph.setScale(20);
		JScrollPane scroll = new JScrollPane(somaGraph.getPanel());
		graphicPanel.add(scroll, BorderLayout.CENTER);
		JPanel panelSouth = new JPanel();
		Border etched = BorderFactory.createEmptyBorder();
        Border titled = BorderFactory.createTitledBorder(
            etched, 
            Resources.getString("zoom")+" (%)",
            TitledBorder.CENTER, 
            TitledBorder.TOP, 
            Resources.getFont(Resources.FONT_TYPE.SLIDER_ZOOM)
        );
        panelSouth.setBorder(titled);
		JSlider slider = new JSlider( SwingConstants.VERTICAL, 0, 100, somaGraph.getScale());
		slider.setFont(Resources.getFont(Resources.FONT_TYPE.SLIDER_ZOOM));
		slider.setMajorTickSpacing(25);
		slider.setMinorTickSpacing(5);
		slider.setPaintTicks(true);
		slider.setPaintLabels(true);
		slider.addChangeListener(new
			ChangeListener()
			{
				public void stateChanged(ChangeEvent event)
				{
					JSlider source = (JSlider) event.getSource();
					somaGraph.setScale(source.getValue());
				}
			}
		);
		panelSouth.add(slider);
		graphicPanel.add( panelSouth, BorderLayout.EAST);
		return graphicPanel;
	}
	private JPanel getTablePanel()
	{
		JPanel tablePanel = new JPanel(new BorderLayout());
		compareTable = new JTable(new CompareTableModel(null));
		compareTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
		JScrollPane scroll = new JScrollPane(compareTable);
		tablePanel.add(scroll, BorderLayout.CENTER);
		return tablePanel;
	}
	private void addPointsToGraph(Date[] date)
	{
		for(int i = 0; i < date.length; i++)
		{
			patient.setDate(date[i]);
			double endomorphy = NutritionForm.endomorphy(
	            patient.getAnthropometryValue(ID.A.SIZE),
	            patient.getAnthropometryValue(ID.A_CREASE.TRICEP),
	            patient.getAnthropometryValue(ID.A_CREASE.SUBSCAPULARIS),
	            patient.getAnthropometryValue(ID.A_CREASE.SUPRAILIAC)
	        );
	        double mesomorphy = NutritionForm.mesomorphy(
	            patient.getAnthropometryValue(ID.A_DIAMETER.HUMERAL),
	            patient.getAnthropometryValue(ID.A_DIAMETER.FEMORAL),
	            patient.getAnthropometryValue(ID.A_GIRTH.CONTRACTED_ARM),
	            patient.getAnthropometryValue(ID.A_GIRTH.LEG),
	            patient.getAnthropometryValue(ID.A.SIZE),
	            patient.getAnthropometryValue(ID.A_CREASE.TRICEP),
	            patient.getAnthropometryValue(ID.A_CREASE.LEG)
	        );
	        double ectomorphy = NutritionForm.ectomorphy(
	            patient.getAnthropometryValue(ID.A.SIZE),
	            patient.getAnthropometryValue(ID.A.CURRRENT_WEIGHT)
	        );
	        somaGraph.addPoint(endomorphy, mesomorphy, ectomorphy, Resources.getDateFormat(date[i]));
		}
	}
	private void addRowsToTable(Date[] days)
	{
		compareTable.setModel(new CompareTableModel(days));
	}
	private class CompareTableModel extends AbstractTableModel
	{
		public CompareTableModel(Date[] days_)
		{
			days = days_;
		}
		public String getColumnName(int c) 
		{ 
			return Resources.getString(columnName[c]); 
		}
		public int getRowCount() 
		{
			if(days == null)
				return 0;
			else
				return days.length;
		}
		public int getColumnCount() 
		{
			return columnName.length;
		}
		public Object getValueAt(int rowIndex, int columnIndex) 
		{			
			if(days == null)
				return null;
			patient.setDate(days[rowIndex]);
			switch(columnIndex)
			{
				case 0:
					return Resources.getDateFormat(patient.getDate());
				case 1:
					return patient.getAnthropometryValue(ID.A.CURRRENT_WEIGHT);
				case 2:
					return patient.getAnthropometryValue(ID.A.TARGET_WEIGHT);
				case 3:
					return patient.getAnthropometryValue(ID.A.SIZE);
				case 4:
					return patient.getAnthropometryValue(ID.A_CREASE.SUBSCAPULARIS);
				case 5:
					return patient.getAnthropometryValue(ID.A_CREASE.TRICEP);
				case 6:
					return patient.getAnthropometryValue(ID.A_CREASE.BICEP);
				case 7:
					return patient.getAnthropometryValue(ID.A_CREASE.FOREARM);
				case 8:
					return patient.getAnthropometryValue(ID.A_CREASE.SUPRASPINAL);
				case 9:
					return patient.getAnthropometryValue(ID.A_CREASE.SUPRAILIAC);
				case 10:
					return patient.getAnthropometryValue(ID.A_CREASE.ABD_LEFT);
				case 11:
					return patient.getAnthropometryValue(ID.A_CREASE.ABD_RIGHT);
				case 12:
					return patient.getAnthropometryValue(ID.A_CREASE.THIGH);
				case 13:
					return patient.getAnthropometryValue(ID.A_CREASE.LEG);
				case 14:
					return patient.getAnthropometryValue(ID.A_CREASE.PECTORAL);
				case 15:
					return patient.getAnthropometryValue(ID.A_GIRTH.RELAXED_ARM);
				case 16:
					return patient.getAnthropometryValue(ID.A_GIRTH.CONTRACTED_ARM);
				case 17:
					return patient.getAnthropometryValue(ID.A_GIRTH.FOREARM);
				case 18:
					return patient.getAnthropometryValue(ID.A_GIRTH.WRIST);
				case 19:
					return patient.getAnthropometryValue(ID.A_GIRTH.CHEST);
				case 20:
					return patient.getAnthropometryValue(ID.A_GIRTH.ABDOMINAL);
				case 21:
					return patient.getAnthropometryValue(ID.A_GIRTH.GLUTEAL);
				case 22:
					return patient.getAnthropometryValue(ID.A_GIRTH.THIGH);
				case 23:
					return patient.getAnthropometryValue(ID.A_GIRTH.LEG);
				case 24:
					return patient.getAnthropometryValue(ID.A_DIAMETER.HUMERAL);
				case 25:
					return patient.getAnthropometryValue(ID.A_DIAMETER.FEMORAL);
				case 26:
					return patient.getAnthropometryValue(ID.A_DIAMETER.BIOESTILOIDEO);
				default:
					return "";
			}
		}
		private final String[] columnName = {
			"date","currentWeight","targetWeight","size","subscapularis",
			"tricep","bicep","forearm","supraspinal","suprailiac",
			"abdLeft","abdRight","thigh","leg","pectoral","relaxedArm",
			"contractedArm","wrist","chest","abdominal","gluteal",
			"humeral","femoral","bioestiloideo"
		};
		private Date[] days;
	}
	private JTable compareTable;
	private Patient patient;
	private Somatochart somaGraph;
}